import TableButtonType from '@/framework/constant/TableButtonType'
import ButtonFuncTypeEnum from '@/framework/constant/ButtonFuncTypeEnum'
import CheckStatusEnum from '@/infra/constant/CheckStatusEnum'
import AuditEnum from '@/framework/constant/auditEnum'

export default class RouterButtonsBuilder {


  static builder(modelName, title = '') {
    return new RouterButtonsBuilder(modelName, title)
  }

  constructor(modelName, title = '') {
    this._modelName = modelName
    this._buttons = []
    this.title = title
  }

  buttonName(name) {
    if (!name) {
      return this._modelName;
    }
    if (name.includes(':')) {
      return name;
    }
    return `${this._modelName}:${name}`
  }

  setTitle(title) {
    this.title = title
    return this
  }

  addButton({
              name = '',
              label,
              handler = '',
              groupId = 0, // 分组功能，默认第0组
              type = TableButtonType.ROW,
              buttonType = 'primary',
              icon,
              showFilter = null,
              col = null,
              buttonFuncType = ButtonFuncTypeEnum.NORMAL,
              funcOption = null
            }) {
    name = this.buttonName(name || handler)
    this._buttons.push({
      name,
      label,
      groupId,
      handler,
      showFilter,
      col,
      type,
      buttonType,
      icon,
      buttonFuncType,
      funcOption
    })
    return this
  }

  addImport(action) {
    this.addButton({
      handler: 'import',
      type: TableButtonType.SEARCHBAR,
      label: '导入',
      buttonFuncType: ButtonFuncTypeEnum.UPLOAD_FILE,
      funcOption: {
        action
      },
      buttonType: 'primary',
      icon: 'el-icon-upload'
    })
    return this
  }

  addDownloadTemplate(action) {
    let option = action
    if (typeof action !== 'object') {
      option = {
        funcOption: {
          action
        }
      }
    }
    this.addButton({
      label: '下载模板',
      handler: 'downloadTemplate',
      buttonFuncType: ButtonFuncTypeEnum.DOWNLOAD_FILE,
      funcOption: {
        action
      },
      icon: 'el-icon-download',
      type: TableButtonType.SEARCHBAR,
      ...option
    })
    return this
  }

  addButtons(ops = []) {
    ops.forEach(op => {
      this.addButton(op)
    })
    return this
  }

  // 查
  r({exported = true, listName = ''} = {}) {
    this.addButton({
      handler: 'list',
      name: listName,
      type: TableButtonType.SEARCHBAR,
      label: '搜索',
      icon: 'el-icon-search'
    })
    if (exported) {
      this.addButton({
        handler: 'export',
        name: 'export',
        // name: listName,
        type: TableButtonType.TOOLBAR_RIGHT,
        label: '导出',
        buttonType: 'primary',
        icon: 'el-icon-download'
      })
    }
    return this
  }

  view(name = '') {
    this.addButton({
      handler: 'view',
      name,
      type: TableButtonType.ROW,
      label: '详情',
      icon: 'el-icon-view'
    })
    return this
  }

  d() {
    this.addButton({
      handler: 'remove',
      type: TableButtonType.ROW,
      label: '删除',
      buttonType: 'danger',
      icon: 'el-icon-delete'
    })
    return this
  }

  e() {
    this.addButton({handler: 'update', type: TableButtonType.ROW, label: '修改', icon: 'el-icon-edit'})
    return this
  }

  curdoo({exported = true, batchOO = true, hasName = true} = {}) {
    this.curd({exported, hasName})
    this.oo({batchOO, hasName})
    return this
  }

  addCheck(batchOps = true, hasName = false) {
    this.addButtons([
      {
        handler: 'check-resolve',
        name: hasName ? 'check' : '',
        type: TableButtonType.ROW,
        label: '审核通过',
        showFilter: row => row.checkStatus !== CheckStatusEnum.RESOLVE.id
      },
      {
        handler: 'check-reject',
        name: hasName ? 'check' : '',
        type: TableButtonType.ROW,
        label: '审核拒绝',
        buttonType: 'danger',
        showFilter: row => row.checkStatus !== CheckStatusEnum.REJECT.id
      }
    ])
    if (batchOps) {
      this.addButtons([
        {handler: 'batch-check-resolve', type: TableButtonType.TOOLBAR, label: '批量审核通过'},
        // BatchCheckReject
        {handler: 'batch-check-reject', type: TableButtonType.TOOLBAR, label: '批量审核拒绝', buttonType: 'danger'}
      ])
    }
    return this
  }
  addAudit(batchOps = true, hasName = false) {
    this.addButtons([
      {
        handler: 'audit-resolve',
        name: hasName ? 'check' : '',
        type: TableButtonType.ROW,
        label: '通过',
        showFilter: row => row.audit === AuditEnum.WAIT_AUTH.id
      },
      {
        handler: 'audit-reject',
        name: hasName ? 'check' : '',
        type: TableButtonType.ROW,
        label: '拒绝',
        buttonType: 'danger',
        showFilter: row => row.audit === AuditEnum.WAIT_AUTH.id
      }
    ])
    // if (batchOps) {
    //   this.addButtons([
    //     {handler: 'batch-check-resolve', type: TableButtonType.TOOLBAR, label: '批量审核通过'},
    //     // BatchCheckReject
    //     {handler: 'batch-check-reject', type: TableButtonType.TOOLBAR, label: '批量审核拒绝', buttonType: 'danger'}
    //   ])
    // }
    return this
  }
  // 增删改查
  curd({exported = true, hasRefresh = true, hasName = false} = {}) {
    hasRefresh && this.r({exported, listName: hasName ? 'list' : ''})
    const ops = [
      {
        handler: 'create',
        name: hasName ? 'create' : '',
        type: TableButtonType.TOOLBAR,
        label: '新建' + this.title,
        icon: 'el-icon-plus'
      },
      {
        handler: 'update',
        name: hasName ? 'update' : '',
        type: TableButtonType.ROW,
        label: '修改',
        icon: 'el-icon-edit'
      },
      {
        handler: 'remove',
        name: hasName ? 'remove' : '',
        type: TableButtonType.ROW,
        label: '删除',
        buttonType: 'danger',
        icon: 'el-icon-delete'
      },
      {
        handler: 'batch-remove',
        name: hasName ? 'batch-remove' : '',
        type: TableButtonType.TOOLBAR,
        label: '批量删除',
        buttonType: 'danger'
      }
    ]
    ops.forEach(op => {
      this.addButton(op)
    })
    return this
  }

  curdNoBatch({exported = true, hasRefresh = true, hasName = true} = {}) {
    return this.curd({exported, hasRefresh, hasName}).removeButton('batch-remove')
  }

  removeButton(name) {
    const index = this._buttons.findIndex(el => el.handler === name)
    if (index > -1) {
      this._buttons.splice(index, 1)
    }
    return this
  }

  reLabel(name, label = null) {
    if (typeof name === 'object') {
      Object.keys(name).forEach(key => {
        this.reLabel(key, name[key])
      })
      return this
    }
    name = this.buttonName(name)
    this._buttons.filter(el => el.name === name).forEach(item => {
      item.label = label
    })
    return this
  }

  // online and offline
  oo({batchOO = true, hasName = true} = {}) {
    let ops = [
      {handler: 'online', type: TableButtonType.ROW, label: '上线', name: hasName ? 'online' : ''},
      {handler: 'offline', type: TableButtonType.ROW, label: '下线', buttonType: 'danger', name: hasName ? 'offline' : ''}
    ]
    if (batchOO) {
      ops = ops.concat([
        {handler: 'batch-online', type: TableButtonType.TOOLBAR, label: '批量上线', name: hasName ? 'online' : ''},
        {
          handler: 'batch-offline',
          type: TableButtonType.TOOLBAR,
          label: '批量下线',
          buttonType: 'danger',
          name: hasName ? 'online' : ''
        }
      ])
    }
    ops.forEach(op => {
      this.addButton(op)
    })
    return this
  }

  ss() {
    const ops = [
      {handler: 'set-as-top', type: TableButtonType.ROW, label: '置顶'},
      {handler: 'set-sort-num', type: TableButtonType.ROW, label: '设置排序', buttonType: 'danger'}
    ]
    ops.forEach(op => {
      this.addButton(op)
    })
    return this
  }

  addShowFlagOp() {
    this.addButton({
      handler: 'batchSetShowFlag',
      label: '批量设置前端显示',
      type: TableButtonType.TOOLBAR
    })
      .addButton({
        handler: 'batchCancelShowFlag',
        label: '批量取消前端显示',
        type: TableButtonType.TOOLBAR,
        buttonType: 'danger'
      })
      .addButton({
        handler: 'setShowFlag',
        label: '设置前端显示',
        type: TableButtonType.ROW,
        showFilter: row => row.showFlag === 0
      })
      .addButton({
        handler: 'cancelShowFlag',
        label: '取消前端显示',
        type: TableButtonType.ROW,
        showFilter: row => row.showFlag === 1,
        buttonType: 'danger'
      })
    return this
  }

  build() {
    if (!this._buttons.length) {
      this.r({exported: false, listName: ''})
    }
    return this._buttons
  }
}
